//tslint:disable
import { Component, OnInit, Input } from '@angular/core';
import { ActionSheetController, AlertController } from "@ionic/angular";
import { Subscription } from 'rxjs';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { MessageService } from 'src/app/shared/service/message.service';
import { EventService } from 'src/app/shared/service/event.service';
import { ModalController } from '@ionic/angular';
import { RemarksContentComponent } from '../remarks-content/remarks-content.component';
import { SharePageComponent } from '../share-page/share-page.component';
import { ActionSheetService, Modal, ModalService } from 'ng-zorro-antd-mobile';
import { RecommendModalComponent } from '../recommend-modal/recommend-modal.component';
import { ServiceModuleComponent } from '../service-module/service-module.component';
import { ModalFromCenterLeave, ModalFromCenterEnter } from 'src/app/utils/transitions';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
@Component({
  selector: 'app-shop-goods-manage-list',
  templateUrl: './shop-goods-manage-list.component.html',
  styleUrls: ['./shop-goods-manage-list.component.scss'],
})
export class ShopGoodsManageListComponent implements OnInit {
  @Input() tagData: any;
  pageNo = 1;
  pageSize: number;
  pageTotal: number;
  tempEvent: any;
  // 筛选及页面数据参数
  params: any = { content: {} };
  sessionId: string;
  moreSelect = false;
  recommendeIds: any;
  tempItem: any;
  listData: any;
  dataIndex: any;
  phoneArray = [];
  icons = [];
  hint = '暂无数据';
  showLoadMore = false;
  // 声明订阅对象
  subscription: Subscription = null;
  ids: any;
  submitContent: any;
  type33Ids = [];
  setCustomBoxVisibility = 'true';// 2024.01.24 新增参数
  constructor(
    private appGlobalService: AppGlobalService,
    private httpService: HttpService,
    private message: MessageService,
    private actionSheetCtrl: ActionSheetController,
    private actionSheet: ActionSheetService, // 必须有
    private alertController: AlertController,
    private eventService: EventService,
    private pageModalServcie: PageModalService,
    public modalController: ModalController,

  ) {

    // 监听多选后
    if (this.eventService.eventEmit.listenerCount('chooseAll') > 0) {
      this.eventService.eventEmit.removeListener('chooseAll');
    }
    // 全选
    this.eventService.eventEmit.on('chooseAll', res => {
      if (res) {
        if (this.tagData.data.footDiv && this.tagData.data.footDiv.detail) {
          this.type33Ids = [];
        }
        this.listData.forEach(element => {
          element.checked = true;
          if (this.tagData.data.footDiv && this.tagData.data.footDiv.detail) {
            this.type33Ids.push(element.id);
          }
        });
        if (this.tagData.data.footDiv && this.tagData.data.footDiv.detail) {
          if (this.type33Ids && this.type33Ids.length > 0) {
            this.appGlobalService.eventService.eventEmit.emit('type33selectedIds', this.type33Ids);
          }
        }
      } else {
        this.listData.forEach(element => {
          element.checked = false;
        });
      }
    });

    // 监听多选后的推荐
    if (this.eventService.eventEmit.listenerCount('addPlatformCaseNow') > 0) {
      this.eventService.eventEmit.removeListener('addPlatformCaseNow');
    }
    this.eventService.eventEmit.on('addPlatformCaseNow', res => {
      const ids = [];
      this.listData.forEach(element => {
        if (element.checked) {
          // ids.push(element[this.tagData.data.chooseSubmit.formKey]);
          ids.push(element.id);
        }
      });

      if (ids.length > 0) {
        let paramsContent = {};
        if (this.tagData.data.footDiv && this.tagData.data.footDiv.chooseSubmit.params.link.params) {
          paramsContent = this.tagData.data.footDiv.chooseSubmit.params.link.params;
        }
        paramsContent['id'] = ids.toString();
        const params = {
          content: paramsContent,
          id: this.tagData.data.submit.id,
          sessionId: localStorage.getItem("sessionId")
        };
        this.httpService.post("application/submit", params, res => {
          this.appGlobalService.showToast(res.message, 1500, "middle");
          if (res.success) {
            this.appGlobalService.popAllTarget();
            this.appGlobalService.goBack();
            //  this.appGlobalService.reloadCurrentPage();
            // if (item.link && item.link.target) {
            //   const pageParams = {
            //     pageId: item.link.target,
            //     params: item.link.params ? item.link.params : {},
            //     sessionId: localStorage.getItem("sessionId")
            //   };
            //   this.appGlobalService.goToForward(pageParams);
            // }
          }
        });
      }

    });

    // 监听多选后的推荐
    if (this.eventService.eventEmit.listenerCount('recommendNow') > 0) {
      this.eventService.eventEmit.removeListener('recommendNow');
    }
    this.eventService.eventEmit.on('recommendNow', res => {

      const ids = [];
      this.listData.forEach(element => {
        if (element.checked) {
          ids.push(element[this.tagData.data.chooseSubmit.formKey]);
        }
      });
      if (ids.length > 0) {
        this.moreSelect = true;
        this.recommendeIds = ids.join(',');
        this.presentModal({
          minLength: 5,
          maxLength: 50,
          value: '',
          placeHolder: '请填写推荐说明，说明将展示给推荐客户',
          button: '确定'
        });

      }

    });
    // 全选
    this.eventService.eventEmit.on('recommendCheckedAll', res => {
      if (res) {
        this.listData.forEach(element => {
          element.checked = true;
        });
      } else {
        this.listData.forEach(element => {
          element.checked = false;
        });
      }
    });
  }

  showModal(link) {
    const that = this;

    link.params.id = this.ids;

    this.pageModalServcie.openPage(link, '', result => {
      if (result) {
        console.log('test' + JSON.stringify(result));
        // that.updateItem();
      }
    });

  }
  async presentAlertWithMsgBtn(submit) {
    const that = this;
    const alert = await this.alertController.create({
      message: submit.text,
      buttons: [
        {
          text: "取消",
          role: "cancel",
          cssClass: "secondary"
        },
        {
          text: '确定',
          handler: () => {
            if (submit.content.listKey) {
              submit.content[submit.content.listKey] = this.ids;
            } else if (submit.content.newListKeyName) {
              submit.content = {};
              let tempContent;
              if (this.submitContent) {
                tempContent = Object.assign(submit.content, this.submitContent);
              }
              submit.content['id'] = this.ids;
            } else {
              submit.content.id = this.ids;
            }
            const params = {
              content: submit.content,
              id: submit.id,
              sessionId: localStorage.getItem("sessionId")
            };
            this.httpService.post("application/submit", params, res => {
              if (res.success) {
                if (submit && submit.content.updateType && submit.content.updateType === 'deleteItem') {
                  this.listData = this.listData.filter((item)=> item.checked !== true);
                  this.tagData.data.data = this.listData;
                } else {
                  that.appGlobalService.reloadCurrentPage();
                }
                this.eventService.eventEmit.emit('submitRes', JSON.stringify({ res, submit }));
              }
              if (res.message && res.message.length > 0) {
                this.appGlobalService.showToast(res.message, 1500, "middle");
              }

            });
          }
        }
      ]
    });
    await alert.present();
  }

  refreshListData(param) {
    const that = this;
    if (param === undefined) {
      return;
    }
    this.pageNo = 1;
    if (!param.showName) {
      if (this.tagData.data.moreButton && this.tagData.data.moreButton.link.params.showName) {
        param.showName = this.tagData.data.moreButton.link.params.showName;
      } else if (this.tagData.data.submit && this.tagData.data.submit.content.showName) {
        param.showName = this.tagData.data.submit.content.showName;
      }
    }
    that.params.content = param;
    that.params.content.pageNo = this.pageNo;
    that.params.sessionId = localStorage.getItem('sessionId');
    this.httpService.post('application/submit', that.params, res => {
      if (res.success) {
        // this.tagData.data.data = [];
        // this.listData = res.data.data; // JSON.parse(res.data.data);
        // if (res.data.submit) {
        //   this.params = res.data.submit; // 为了每次刷新下一页的参数 "oldIndex"
        // }

        this.listData = res.data.data;
        if (res.data.moreButton) {
          this.tagData.data.moreButton = res.data.moreButton;
        }
        this.pageTotal = this.tagData.pageTotal;

        this.showLoadMore = !this.tagData.data.moreButton && this.tagData.data.submit && this.pageTotal && (this.pageNo < Math.ceil(this.pageTotal / this.pageSize)) && this.tagData.asyncShow !== 'true';
      } else {
        // 请求数据失败
        this.tagData.data.data = [];
        this.appGlobalService.showToast(res.message, 2000, 'middle');
      }
    },
      err => {
        // 请求数据失败
        this.tagData.data.data = [];
        this.appGlobalService.showToast(err.message, 2000, 'middle');
      });
  }

  ngOnInit() {
    this.type33Ids = [];
    sessionStorage.removeItem('showName');
    const listenerName = this.tagData.name + 'AskForList';
    this.eventService.eventEmit.on(listenerName, (obj) => {
      this.refreshListData(obj);
    });



    // console.log(this.tagData);
    this.sessionId = localStorage.getItem('sessionId');
    if (this.tagData.data.data !== undefined) {
      this.listData = this.tagData.data.data;
      this.listData.forEach(element => {
        if (element.checked !== undefined && element.checked === 'true') {
          element.checked = true;
        } else {

        }
      });
    }

    // 2024.01.24 新增参数联调
    if (this.tagData.data && this.tagData.data && this.tagData.data.data) {
      if (this.tagData.data.data[0]
        && this.tagData.data.data[0].enableSetCustomBoxVisibility !== undefined
        && this.tagData.data.data[0].enableSetCustomBoxVisibility === 'true') {
        this.eventService.eventEmit.on('setCustomBoxVisibility', (obj) => {
          this.setCustomBoxVisibility = obj;
        });
      }

      if (this.tagData.data.data[0]
        && this.tagData.data.data[0].setCheckBoxVisibility !== undefined
      ) {
        this.setCustomBoxVisibility = this.tagData.data.setCheckBoxVisibility;
      }        
      if (this.appGlobalService.setCustomBoxVisibility !== undefined) {
        this.setCustomBoxVisibility = this.appGlobalService.setCustomBoxVisibility;
      }

    }

    



    if (this.tagData.data.submit !== undefined) {
      this.params = this.tagData.data.submit;
      this.params.sessionId = this.sessionId;

      this.pageSize = this.tagData.pageSize;
      this.pageTotal = this.tagData.pageTotal;
    }
    if (this.tagData.hint !== undefined) {
      this.hint = this.tagData.hint;
    }

    this.showLoadMore =
      !this.tagData.data.moreButton &&
      this.tagData.data.submit &&
      this.pageTotal &&
      (this.pageNo < Math.ceil(this.pageTotal / this.pageSize)) &&
      this.tagData.asyncShow !== 'true';
    if (!this.subscription && this.tagData.data.submit !== undefined && this.tagData.name.indexOf('_withSiftFilter') > 0) {
      // 1.7.0 add for筛选
      this.message.clearMessage();
      // 获取筛选参数
      this.subscription = this.message.getMessage().subscribe(param => {
        if (param === undefined) {
          return;
        } else if (param.type === 'ionRefresh') {
          this.refreshListData(param);
          return;
        }
        this.pageNo = 1;
        this.params.sessionId = localStorage.getItem("sessionId");

        Object.keys(this.tagData.data.submit.content).forEach(element => {
          // console.log('--->' +element);

          param[element] = this.tagData.data.submit.content[element];
        });
        // if (this.tagData.data.submit && this.tagData.data.submit.content && this.tagData.data.submit.content.showName !== undefined) {
        //   this.appGlobalService.submitShowName = this.tagData.data.submit.content.showName;
        //   param.showName = this.appGlobalService.submitShowName;
        // } else {
        //   param.showName = this.appGlobalService.submitShowName;
        // }



        // 参数
        this.params.content = param;


        // 页面数据参数
        this.params.content.pageNo = this.pageNo;
        // 请求获取新数据
        this.httpService.post(
          "application/submit",
          this.params,
          res => {
            console.log("submit");
            // 请求数据成功
            if (res.success) {
              const result = res.data.data;
              // 筛选数据
              this.listData = result;
            } else {
              // 请求数据失败
              this.appGlobalService.showToast(res.message, 2000, "middle");
            }
          },
          err => {
            // 请求数据失败
            this.appGlobalService.showToast(err.message, 2000, "middle");
          }
        );
      });
    }

    if (this.eventService.eventEmit.listenerCount('auditNow') > 0) {
      this.eventService.eventEmit.removeListener('auditNow');
    }
    this.eventService.eventEmit.on('auditNow', res => {
      console.log(res);
      const ids = [];
      this.listData.forEach((element) => {
        if (element.checked) {
          ids.push(element.id);
          if (element.choseSubmit && element.choseSubmit.box && element.choseSubmit.box.listKey && element.choseSubmit.box.params) {
            this.submitContent = element.choseSubmit.box.params;
          }
        }
      });
      if (ids.length === 0 && !res.noNeedChoose) {
        this.ids = '';
        return;
      }
      this.ids = ids.join(',');

      if (res.target) {
        this.showModal(res);
      } else {
        // submit
        this.presentAlertWithMsgBtn(res);

      }

    });

    if (this.tagData.data.footDiv && this.tagData.data.footDiv.detail && this.tagData.data.footDiv.detail.type) {
      this.eventService.eventEmit.on('removeCustomIds', res => {
        console.log('res', res);
        this.listData.forEach((element, index) => {
          if (element.id === res) {
            // this.refreshListData(obj);
            this.listData.splice(index, 1);
          }
        });
      });
    }
  }

  async presentAction() {
    const buttonsArr = [
      {
        text: "取消",
        cssClass: ["action-sheet-font-color", "action-sheet-font-top"],
        handler: () => {
          actionSheet.dismiss();
        }
      }
    ];
    if (this.phoneArray[1]) {
      buttonsArr.unshift({
        text: this.phoneArray[1],
        cssClass: ["action-sheet-font-color"],
        handler: () => {
          this.presentAlertConfirm(Number(this.phoneArray[1]));
        }
      });
    }

    if (this.phoneArray[0]) {
      buttonsArr.unshift({
        text: this.phoneArray[0],
        cssClass: ["action-sheet-font-color"],
        handler: () => {
          this.presentAlertConfirm(this.phoneArray[0]);
        }
      });
    }

    const actionSheet = await this.actionSheetCtrl.create({
      mode: "md",
      cssClass: "match-padding",
      buttons: buttonsArr
    });
    await actionSheet.present();
  }

  async presentAlertConfirm(mobile, name?) {
    const msg = (name ? name + ' ' : '') + mobile;
    const alert = await this.alertController.create({
      header: this.tagData.nickName,
      message: msg,
      buttons: [
        {
          text: "取消",
          role: "cancel",
          cssClass: "secondary"
        },
        {
          text: "拨打",
          handler: () => {
            this.httpService.changeCallNumber(mobile);
          }
        }
      ]
    });

    await alert.present();
  }


  // 分享弹框
  async presentShareModal(shareData) {
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: 'sharePage',
      backdropDismiss: true,
      component: SharePageComponent,
      componentProps: { tagData: shareData }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    console.log(modalData);
  }
  iconClick(item, dataIndex?) {
    if (dataIndex >= 0) {
      this.dataIndex = dataIndex;
    }

    if (item.modelType === "iconLink") {
      if (item.link.type && item.link.type === 'chat') {//
        this.httpService.gotoChat(item);
      } else if (item.label === '分享') {
        const params = {
          sessionId: localStorage.getItem("sessionId"),
          pageId: item.link.target,
          params: item.link.params
        };
        this.httpService.post('application/page', params, res => {
          console.log(JSON.parse(res.script));
          const tmpShareData = JSON.parse(res.script).views[0].tags[0].data;
          // console.log(tmpShareData);
          this.presentShareModal(tmpShareData);
        });
      } else {
        // 蜂喔商户 1.6.29 新增需求
        this.pageModalServcie.openPage(item.link, '', res => {
          if (res) {
            if (item.link && item.link.params && item.link.params.ids) {
              this.updateItem(item.link.params.ids, item.link.params.refreshType, dataIndex);
            } else if (item.link && item.link.params && item.link.params.refreshType && item.link.params.goods_id) {
              this.updateItem(item.link.params.goods_id, item.link.params.refreshType, dataIndex);
            }
          }
        });
      }
    } else if (item.modelType === "iconSubmit") {
      const params = {
        content: item.submit.content,
        id: item.submit.id,
        sessionId: localStorage.getItem("sessionId")
      };
      this.httpService.post("application/submit", params, res => {
        if (item.submit.content.showSuccesIcon) {
          this.appGlobalService.showToast(res.message, 1500, "middle", item.submit.content.showSuccesIcon);
        } else {
          this.appGlobalService.showToast(res.message, 1500, "middle");
        }
        if (res.success) {
          if (item.submit && item.submit.content && item.submit.content.refreshType) {
            this.updateItem(item.submit.content.ids, item.submit.content.refreshType, dataIndex);
          } else {
            this.appGlobalService.reloadCurrentPage();
          }
          if (item.link && item.link.target) {
            const pageParams = {
              pageId: item.link.target,
              params: item.link.params ? item.link.params : {},
              sessionId: localStorage.getItem("sessionId")
            };
            this.appGlobalService.goToForward(pageParams);
          }
        }
      });
    } else if (item.modelType === 'iconDelete') {
      // 删除
      const params = {
        content: item.submit.content,
        id: item.submit.id,
        sessionId: localStorage.getItem("sessionId")
      };
      this.httpService.post("application/submit", params, res => {
        this.appGlobalService.showToast(res.message, 1500, "middle");
        if (res.success) {
          if (this.dataIndex >= 0 && this.dataIndex < this.tagData.data.data.length) {
            this.tagData.data.data.splice(this.dataIndex, 1);
          }
        }
      });
    } else if (item.modelType === "phone") {
      // 电话
      this.phoneArray = item.phone.split(",");
      if (this.phoneArray.length > 1) {
        this.presentAction();
      } else {
        this.presentAlertConfirm(Number(this.phoneArray[0]), item.contactName ? item.contactName : '');
      }
    } else if (item.modelType === "dialog") {
      // 备注
      // this.remarksIcon = item;
      // this.openRemarks = true;
      this.presentModal2("remarks", item);
    }
    // 商铺列表新增类型
    else if (item.modelType === 'pageDialog') {
      this.openTipsPageModal("recommend-modal", item);
    }
    // 新增两个components类型
    else if (item.modelType === "remarkList") {
      // tslint:disable-next-line:max-line-length
      // const moreObj = JSON.parse('{"layout":"right","data":[{"submit":{"id":"refresh","content":{"opportunity_id":"5eb3aed42872fb0001f76ed7"}},"action":"submit","label":"刷新","paramName":"opportunity_id"},{"link":{"type":"page","params":{"opportunity_id":"5eb3aed42872fb0001f76ed7"},"target":144348},"action":"link","label":"修改"}],"icon":"nav_icon_more@3x.png"}');
      if (item.listRemark) {
        // this.remarkList(item);
      }
    }
    else if (item.modelType === "serviceModal") {
      this.openSeviceModal("sevice-modal", item);
    }
    else if (item.modelType === "childIconLink") {
      if (item.childIconList) {
        // this.remarkList(item);
      }
    } else if (item.modelType === "buttonLink") {
      // 蜂喔商户 1.6.32 新增需求
      this.pageModalServcie.openPage(item.link, '', res => {
        if (res) {
          if (item.link && item.link.params && item.link.params.ids) {
            this.updateItem(item.link.params.ids, item.link.params.refreshType, dataIndex);
          } else if (item.link && item.link.params && item.link.params.refreshType && item.link.params.goods_id) {
            this.updateItem(item.link.params.goods_id, item.link.params.refreshType, dataIndex);
          }
        }
      });
    }
  }

  // 只刷新单条记录
  updateItem(tempId, refreshType, dataIndex) {
    const that = this;
    if (!tempId) {
      return;
    }
    if (refreshType === 'deleteItem') {
      that.listData.splice(dataIndex, 1);
    } else {
      const page: any = this.appGlobalService.getCurrentTarget();
      if (page.params) {
      } else {
        page.params = {}
      }
      page.params.id = tempId;
      this.httpService.post('application/page', page, async res => {
        const tagData = JSON.parse(res.script).views[0].tags;
        tagData.forEach(element => {
          if (element.style === 'shopGoodsManageList' && element.data && element.data.data[0]) {
            that.tagData.data.data.splice(that.dataIndex, 1, element.data.data[0]);
          }
        });
      });
    }

  }

  // 单选 推荐
  recommentAction(item) {
    this.moreSelect = false;
    this.tempItem = item;
    this.recommendeIds = item[this.tagData.data.chooseSubmit.formKey];
    this.presentModal({ minLength: '5', maxLength: '50', value: '', placeHolder: '请填写推荐说明，说明将展示给推荐客户', button: '确定' });
  }

  recommentPhoneAction(phone) {
    this.httpService.changeCallNumber(phone);
  }


  async presentModal(textarea) {
    console.log("item", textarea);
    const modal = await this.modalController.create({
      showBackdrop: true,
      // backdropDismiss: false,
      cssClass: 'remarks',
      component: RemarksContentComponent,
      componentProps: { textarea, type: 'textarea' }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    console.log(modalData, "moddddddd");
    if (!modalData.data) {
      return;
    }
    this.recommendWithRemark(modalData.data.value);
  }

  async presentModal2(value, item) {
    console.log("item", item);
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: value,
      component: RemarksContentComponent,
      componentProps: { item, type: value }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    console.log(modalData, "moddddddd");
  }
  async openTipsPageModal(className, item: any) {
    try {
      const params = {
        sessionId: localStorage.getItem("sessionId"),
        pageId: item.link.target,
        params: item.link.params
      };
      this.httpService.post('application/page', params, async res => {
        const tagData = JSON.parse(res.script).views[0].tags[0].data;
        if (tagData) {
          const modal = await this.modalController.create({
            showBackdrop: true,
            cssClass: className,
            animated: false,
            component: RecommendModalComponent,
            componentProps: { tagData },
            leaveAnimation: ModalFromCenterLeave,
            enterAnimation: ModalFromCenterEnter
          });
          await modal.present();
          const modalData = await modal.onDidDismiss().then((data) => {
            this.appGlobalService.reloadCurrentPage();
          }); // 获取关闭传回的值
        }
      });
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
    }
  }

  async openSeviceModal(className, item) {
    const type = {
      "sevice-modal": ServiceModuleComponent,
    };
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: className,
      component: type[className],
      componentProps: { tagData: item }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
  }
  recommendWithRemark(value) {
    if (value.length < 1) {
      return;
    }
    const params = {
      sessionId: this.sessionId,
      content: this.tagData.data.chooseSubmit.content,
      id: this.tagData.data.chooseSubmit.id,
    };
    params.content[this.tagData.data.chooseSubmit.formKey] = this.recommendeIds;
    params.content.remark = value;
    this.httpService.post('application/submit', params, res => {
      if (res.success) {
        if (this.tagData.data.chooseSubmit.link && this.tagData.data.chooseSubmit.link.target.length > 0) {
          this.appGlobalService.openPage(this.tagData.data.chooseSubmit.link);
        } else {
          this.appGlobalService.showToast(res.message, 1500, 'middle');
          if (this.moreSelect) {
            this.listData.forEach(element => {
              if (element.checked) {
                element.fourthRowLeft.firstText.label = "刚刚已推荐";
              }
            });
          } else {
            this.tempItem.fourthRowLeft.firstText.label = "刚刚已推荐";
          }
        }

      } else {
        this.appGlobalService.showToast(res.message ? res.message : '推荐失败', 1500, 'middle');
        if (res.message && res.message === '推荐说明含有非法词') {
          this.presentModal({ minLength: 5, maxLength: 50, value, placeHolder: '请填写推荐说明，说明将展示给推荐客户', button: '确定' });
        }
      }

    });
  }

  choose(item) {
    item.checked = !item.checked;
    if (this.tagData.data.footDiv && this.tagData.data.footDiv.detail && this.tagData.data.footDiv.detail.action) {
      this.type33Ids = [];
      this.listData.forEach(element => {
        if (element.checked) {
          this.type33Ids.push(element.id);
        }
      });
      this.appGlobalService.eventService.eventEmit.emit('type33selectedIds', this.type33Ids);
    }
    this.checkAll();
  }
  // 检查是否已经全选
  checkAll() {
    if (this.tagData.data && this.tagData.data.footDiv && this.tagData.data.footDiv.chooseSubmit !== undefined) {
      this.tagData.data.chooseSubmit = this.tagData.data.footDiv.chooseSubmit;
    }
    // max 10
    if (this.tagData.data.chooseSubmit && this.tagData.data.chooseSubmit.type && this.tagData.data.chooseSubmit.type === 'ljpopup') {
      this.tagData.data.chooseSubmit.maxRow = this.tagData.data.chooseSubmit.maxRow > 0 ? this.tagData.data.chooseSubmit.maxRow : 10;
      let count = 0;
      this.listData.forEach(element => {
        if (element.checked) {
          ++count;
          // console.log('selectCount=', count);
          if (count > this.tagData.data.chooseSubmit.maxRow) {
            element.checked = false;
            this.appGlobalService.showToast('最多只能选择' + this.tagData.data.chooseSubmit.maxRow + '个', 2000, 'middle');
          } else {
            if (this.tagData.data.footDiv.detail && this.tagData.data.footDiv.detail.action) {
              this.eventService.eventEmit.emit('type33selectedCount', count);
            }
          }
        }
      });
    } else {
      let all = true;
      this.listData.forEach(element => {
        if (!element.checked) {
          all = false;
        }
      });
      this.eventService.eventEmit.emit('recommendAllResult', all);
    }

  }
  detailPage(link, event?) {
    if (event !== undefined) {
      event.stopPropagation();
    }
    if (!link) {
      return;
    }
    const pageParams = {
      pageId: link.target,
      params: link.params,
      sessionId: localStorage.getItem('sessionId')
    };
    this.appGlobalService.goToForward(pageParams);
  }

  call(phone) {
    this.httpService.changeCallNumber(phone);
  }

  loadMoreData(event) {
    try {
      this.params.sessionId = localStorage.getItem("sessionId");
      this.tempEvent = event;
      // // 请求下一个页面
      this.pageNo = this.pageNo + 1;
      // // 参数
      this.params.content.pageNo = this.pageNo;
      // 请求获取新数据
      this.httpService.post('application/submit', this.params, res => {

        // 请求数据成功
        if (res.success) {
          const resList = res.data.data;
          // 追加返回数据
          if (resList && resList.length > 0) {
            // 追加数据
            resList.map(v => {
              this.listData.push(v);
            });
            this.checkAll();
          } else {
            event.target.disabled = true;
          }
        } else {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, 'middle');
        }
        event.target.complete();

      }, err => {
        // 请求失败，更新当前页数
        this.pageNo = this.pageNo - 1;
        // 请求数据失败
        this.appGlobalService.showToast(err.message, 2000, 'middle');
        event.target.complete();

      });
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
      event.target.complete();

    }
  }

  getInterger(value) {
    return parseInt(value);
  }

  ngOnDestroy(): void {
    if (this.subscription) {
      this.subscription.unsubscribe();
    }
    this.eventService.eventEmit.removeListener('recommendNow');
    this.eventService.eventEmit.removeListener('chooseAll');
    this.eventService.removeCustomListListenersByName('removeCustomIds');
    // this.eventService.removeCustomListListeners();
    this.eventService.removeCustomListListenersByName(this.tagData.name + 'AskForList');
  }
}


